import React, {useState, useRef} from 'react'
import {message} from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect} from 'umi'
import { queryManageList,sendPositiveBpm} from '../service'
import {jobLevelTypeList, regularBpmStatusList} from "@/utils/constant";
import {timer} from "@/utils/utils";
import moment from 'moment'
import AddForm from './components/AddForm';
import store from 'store2';

const TableList = () => {
  const actionRef = useRef();
  const [visible, setVisible] = useState(false);
  const [loading,setLoading] = useState(false);
  const [record,setRecord] = useState(null);

  const addApply = (record) =>{
    setVisible(true);
    setRecord(record);
  };

  const handleCancel = () => {
    setVisible(false);
    setRecord(null);
  };

  const handleSubmit = (values) => {
    setLoading(true);
    sendPositiveBpm(values).then(res => {
      if (res.flag){
        window.open(res.data);
        message.success(`发起Bpm成功！`);
        store.remove(`personnel-manage-` + record.id);
        handleCancel();
        timer(actionRef);
      } else {
        message.error(`Bpm错误:`+res.error);
      }
    });
    setLoading(false);
  };

  const columns = [
    {
      title: '序号',
      render:(text,record,index)=>`${index+1}`,
      hideInSearch: true,
    },
    {
      title: '工号',
      hideInSearch: true,
      dataIndex: 'code',
      key:'code',
    },
    {
      title: '姓名',
      dataIndex: 'name',
      hideInSearch: true,
      key:'name'
    },
    {
      title: '申请日期',
      dataIndex: 'createTime',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '晋升类型',
      dataIndex: 'jobLevelType',
      hideInSearch: true,
      render: (text) => {
        const res = jobLevelTypeList.find((item) => item.value === parseInt(text));
        return res ? res.label : text
      },
    },
    {
      title: '所属部门',
      hideInSearch: true,
      dataIndex: 'beforeUnitName',
      key:'beforeUnitName'
    },
    {
      title: '现任岗位',
      dataIndex: 'beforeJobName',
      key:'beforeJobName'
    },
    {
      title: '现职级',
      dataIndex: 'beforeJobLevel',
      hideInSearch: true,
    },
    {
      title: '申请晋升部门',
      dataIndex: 'unitname',
      hideInSearch: true,
    },
    {
      title: '申请晋升岗位',
      dataIndex: 'jobname',
      hideInSearch: true,
    },
    {
      title: '申请晋升职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '申请晋升日期',
      dataIndex: 'applyTime',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '考察期预计结束日期',
      dataIndex: 'regularDay',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '考察到期倒计时',
      dataIndex: 'countdown',
      hideInSearch: true,
      render:(text,record) => {
        let ctm = moment()
        let etm = moment(record.regularDay)
        return etm.diff(ctm, 'days') + "天";
      }
    },
    {
      title: '转正状态',
      dataIndex: 'regularBpmStatus',
      key:'regularBpmStatus',
      hideInSearch: true,
      valueEnum: {
        0: { text: regularBpmStatusList[0].label, status: 'Default' },
        2: { text: regularBpmStatusList[1].label, status: 'Processing' },
        3: { text: regularBpmStatusList[2].label, status: 'Success' },
        4: { text: regularBpmStatusList[3].label, status: 'Default' },
        6: { text: regularBpmStatusList[3].label, status: 'Default' },
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          {(record.regularBpmStatus === 0) && <a onClick={() => addApply(record)}>发起转正申请</a>}
          {(record.regularBpmStatus !== 3 && record.checkUrl) && <><a onClick={() => window.open(record.checkUrl)}>审批详情</a></>}
        </>
      ),
    },
  ];

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        search={false}
        request={(params) => queryManageList(params)}
        columns={columns}
      />
      <AddForm
        loading={loading}
        visible={visible}
        record={record}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />
    </PageHeaderWrapper>
  )
}

export default connect(() => ({
}))(TableList)
